import { defineStore } from "pinia";
import { ref } from "vue";
import type { PlaylistState, Music } from './type'
import { checkSong, getLyric } from "@/api/player";

export const usePlaylistStore = defineStore('playlist', () => {
    const currentPlaylist = ref<PlaylistState>({
        playlist: [{
            albumId: 35069014,
            albumName: "无法长大",
            author: "赵雷",
            id: 447925059,
            name: "阿刁",
            picUrl: "https://p2.music.126.net/-noYXenVuiTrlcxrCqNPjQ==/109951169213362679.jpg",
            publishTime: 1482249600007
        }],//播放列表
        playlistIndex: 0,//默认下标
        isBtnShow: true,//暂停按钮
        detailShow: false,//歌曲详情是否显示
        currentTime: 0,//当前时间
        duration: 0,//歌曲总时长
        musicId: 447925059,//歌曲id
        musicUrl: ``,//歌曲url
        musicLyric: ''//歌词
    });
    // 更新播放列表
    const updatePlaylist = (playlist: Music[]) => {
        currentPlaylist.value.playlist = playlist
    }
    // 更新播放音乐下标
    const updatePlaylistIndex = (index: number) => {
        currentPlaylist.value.playlistIndex = index
    }
    // 更新播放状态
    const updateIsBtnShow = (isBtnShow: boolean) => {
        currentPlaylist.value.isBtnShow = isBtnShow
    }
    // 更新歌曲详情显示状态
    const updateDetailShow = (detailShow: boolean) => {
        currentPlaylist.value.detailShow = detailShow
    }
    // 更新当前播放时间
    const updateCurrentTime = (currentTime: number) => {
        currentPlaylist.value.currentTime = currentTime
    }
    // 更新歌曲总时长
    const updateDuration = (duration: number) => {
        currentPlaylist.value.duration = duration
    }
    // 更新歌曲id
    const updateMusicId = (musicId: number) => {
        currentPlaylist.value.musicId = musicId
    }
    // 更新歌曲url
    const updateMusicUrl = (musicUrl: string) => {
        currentPlaylist.value.musicUrl = musicUrl
    }
    // 更新歌词
    const updateMusicLyric = (musicLyric: string) => {
        currentPlaylist.value.musicLyric = musicLyric
    }
    // 获取歌词和歌曲url
    const getMusicDetail = async (id: number) => {
        updateMusicUrl(`https://music.163.com/song/media/outer/url?id=${id}.mp3`)
        getLyric(id)
            .then((res: any) => {
                updateMusicLyric(res.lrc.lyric)
            })
            .catch((err: any) => {
                console.log(err)
            })
    }
    // 检查歌曲是否可播放
    const checkSongPlay = async (id: number) => {
        const res: any = await checkSong(id)
        console.log(res, 'res');
        if (res.message === 'ok') {
            getMusicDetail(id).then(() => {
                updateIsBtnShow(true)
            })
            return Promise.resolve()
        } else {
            return Promise.reject('亲爱的,暂无版权')
        }
    }
    // 清空当前播放状态
    const clearCurrentPlay = () => {
        updateIsBtnShow(true)
        updateDetailShow(false)
        updateCurrentTime(0)
        updateDuration(0)
        updateMusicUrl('')
        updateMusicLyric('')
    }
    return {
        currentPlaylist,
        updatePlaylist,
        updatePlaylistIndex,
        updateIsBtnShow,
        updateDetailShow,
        updateCurrentTime,
        updateDuration,
        updateMusicId,
        checkSongPlay,
        getMusicDetail,
        updateMusicUrl,
        updateMusicLyric,
        clearCurrentPlay
    }
})